package com.baosight.risk.service.xtzy.impl;

import com.baosight.risk.api.xtzy.IXtzyIntegralService;
import com.baosight.risk.api.zbaa.IZbaaTargetGroupOrderService;
import com.baosight.risk.api.zbaa.IZbaaTargetGroupService;
import com.baosight.risk.api.zbaa.IZbaaTargetService;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.enums.IntegralType;
import com.baosight.risk.common.utils.Convert;
import com.baosight.risk.common.utils.IDGenerator;
import com.baosight.risk.mapper.xtaa.code.XtaaCodeMapper;
import com.baosight.risk.mapper.xtzy.XtzyIntegralDetailMapper;
import com.baosight.risk.mapper.xtzy.XtzyIntegralMapper;
import com.baosight.risk.service.xtaa.code.entity.XtaaCode;
import com.baosight.risk.service.xtzy.entity.XtzyIntegral;
import com.baosight.risk.service.xtzy.entity.XtzyIntegralDetail;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.service.zbaa.entity.ZbaaTarget;
import com.baosight.risk.service.zbaa.entity.ZbaaTargetGroup;
import com.baosight.risk.service.zbaa.entity.ZbaaTargetGroupOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.baosight.risk.common.utils.ShiroUtils.getUser;

/**
 * @desc:
 * @author: CuiShiHao
 **/
@Service
public class XtzyIntegralServiceImpl extends BaseServiceImpl<XtzyIntegral> implements IXtzyIntegralService {

    @Autowired
    private XtzyIntegralMapper integralMapper;

    @Autowired
    private XtzyIntegralDetailMapper integralDetailMapper;

    @Autowired
    private XtaaCodeMapper xtaaCodeMapper;

    @Autowired
    private IZbaaTargetService targetService;

    @Autowired
    private IZbaaTargetGroupService targetGroupService;

    @Autowired
    private IZbaaTargetGroupOrderService targetGroupOrderService;

    @Override
    public void insertIntegral(XtzyUser user, IntegralType type) {
        List<XtaaCode> xtaaCodeList = xtaaCodeMapper.selectAllDataByDDTypeCode("INTEGRAL");
        List<XtaaCode> filterList = xtaaCodeList.stream().filter(o -> o.getDdCode().equals(String.valueOf(type.toValue()))).collect(Collectors.toList());
        if (filterList.size() > 0) {
            XtaaCode xtaaCode = filterList.get(0);
            String title = xtaaCode.getDdName();
            int score = Convert.toInt(xtaaCode.getExt1());
            if (user == null) {
                user = getUser();
            }
            // 排除admin积分
            if (user.getUserCode().equals("admin")) {
                return;
            }

            //插入/更新积分表
            XtzyIntegral integral = new XtzyIntegral();
            integral.setId(IDGenerator.getIdStr());
            integral.setUserCode(user.getUserCode());
            integral.setUserDesc(user.getUserDesc());
            integral.setIntegralSum(score);
            integral.setUpdateTime(new Date());
            if (integralMapper.selectIntegral(integral).size()>0) {
                //修改
                integralMapper.updateIntegral(integral);
            }else{
                //新增
                integralMapper.insertIntegral(integral);
            }

            //插入明细表
            XtzyIntegralDetail integralDetail = new XtzyIntegralDetail();
            integralDetail.setId(IDGenerator.getIdStr());
            integralDetail.setUserCode(user.getUserCode());
            integralDetail.setUserDesc(user.getUserDesc());
            integralDetail.setIntegral(score);
            integralDetail.setSourceName(title);
            integralDetail.setSourceCode(xtaaCode.getDdCode());
            integralDetail.setCreateTime(new Date());
            integralDetailMapper.insert(integralDetail);
        }
    }

    @Override
    public XtzyIntegral getUserIntegral() {
        XtzyIntegral integral = integralMapper.selectIntegralInfoByUserCode(getUser().getUserCode());
        int score = integral.getIntegralSum();
        //获取用户当前积分对应的等级
        List<XtaaCode> xtaaCodeList = xtaaCodeMapper.selectAllDataByDDTypeCode("INTEGRAL_LEVEL");
        List<XtaaCode> collect = xtaaCodeList.stream().sorted(Comparator.comparing(XtaaCode::getExt1).reversed()).collect(Collectors.toList());
        for (XtaaCode xtaaCode : collect) {
            if (Convert.toInt(xtaaCode.getExt1()) <= score) {
                integral.setExt2(xtaaCode.getDdName());
                break;
            }
        }
        return integral;
    }

    /**
     * @description 检查是否超过订阅上限
     */
    @Override
    public boolean checkOrderLimit() {
        XtzyUser user = getUser();
        XtzyIntegral integral = integralMapper.selectIntegralInfoByUserCode(user.getUserCode());
        int score = integral.getIntegralSum();

        List<XtaaCode> xtaaCodeList = xtaaCodeMapper.selectAllDataByDDTypeCode("INTEGRAL_LEVEL");
        List<XtaaCode> collect = xtaaCodeList.stream().sorted(Comparator.comparing(XtaaCode::getExt1).reversed()).collect(Collectors.toList());
        for (XtaaCode xtaaCode : collect) {
            if (Convert.toInt(xtaaCode.getExt1()) <= score) {
                // 获取当前用户全部订阅指标
                List<ZbaaTarget> orderList = targetService.getTargetListByUser(getUser(), "");
                boolean result = orderList.size() >= Convert.toInt(xtaaCode.getExt2()) ? false : true;
                return result;
            }
        }
        return false;
    }

    /**
     * @description 自定义指标看板——检查是否超过创建看板分组上限
     */
    @Override
    public boolean checkGroupLimit() {
        XtzyUser user = getUser();
        XtzyIntegral integral = integralMapper.selectIntegralInfoByUserCode(user.getUserCode());
        int score = integral.getIntegralSum();

        List<XtaaCode> xtaaCodeList = xtaaCodeMapper.selectAllDataByDDTypeCode("INTEGRAL_LEVEL");
        List<XtaaCode> collect = xtaaCodeList.stream().sorted(Comparator.comparing(XtaaCode::getExt1).reversed()).collect(Collectors.toList());
        for (XtaaCode xtaaCode : collect) {
            if (Convert.toInt(xtaaCode.getExt1()) <= score) {
                // 获取当前用户全部自定义指标分组
                List<ZbaaTargetGroup> zbaaTargetGroupList = targetGroupService.selectGroupByUserId(getUser().getUserCode());
                boolean result = zbaaTargetGroupList.size() >= Convert.toInt(xtaaCode.getExt3()) ? false : true;
                return result;
            }
        }
        return false;
    }

    /**
     * @description 自定义指标看板——检查是否超过看板分组订阅指标上限
     */
    @Override
    public boolean checkGroupOrderLimit(String groupId) {
        XtzyUser user = getUser();
        XtzyIntegral integral = integralMapper.selectIntegralInfoByUserCode(user.getUserCode());
        int score = integral.getIntegralSum();

        List<XtaaCode> xtaaCodeList = xtaaCodeMapper.selectAllDataByDDTypeCode("INTEGRAL_LEVEL");
        List<XtaaCode> collect = xtaaCodeList.stream().sorted(Comparator.comparing(XtaaCode::getExt1).reversed()).collect(Collectors.toList());
        for (XtaaCode xtaaCode : collect) {
            if (Convert.toInt(xtaaCode.getExt1()) <= score) {
                // 获取当前用户选择的看板分组中全部指标
                List<ZbaaTargetGroupOrder> zbaaTargetGroupList = targetGroupOrderService.selectGroupOrderByGroupId(groupId);
                boolean result = zbaaTargetGroupList.size() >= Convert.toInt(xtaaCode.getExt4()) ? false : true;
                return result;
            }
        }
        return false;
    }


}
